<template>
  <div class="app-container">
    <div class="filter-container">
      <div style="float: right;">
        <el-button icon="el-icon-plus" circle @click="handleAddRow(null,-1)" />
        <el-button icon="el-icon-refresh" circle @click="handleFilterQuery" />
      </div>
    </div>

    <!-- 数据表格 -->
    <el-table
      :key="tableKey"
      v-loading="loading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      scrollbar-always-on="true"
    >
      <el-table-column label="序号" type="index" width="50px" align="center" />
      <!--按需调整-->
      <el-table-column label="参数属性" min-width="100px" align="center">
        <template v-slot="{row}">
          <el-input v-model="row.resultSource" placeholder="请输入" clearable />
        </template>
      </el-table-column>
      <el-table-column label="参数名称" min-width="100px" align="center">
        <template v-slot="{row}">
          <el-input v-model="row.resultName" placeholder="请输入" clearable />
        </template>
      </el-table-column>
      <el-table-column label="参数类型" min-width="80px" align="center">
        <template v-slot="{row}">
          <el-select v-model="row.resultType" placeholder="请选择" style="width: 100%;" class="filter-item">
            <el-option key="String" label="字符串" value="String" />
            <el-option key="Number" label="数字" value="Number" />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="是否必填" min-width="70px" align="center">
        <template v-slot="{row}">
          <el-select v-model="row.isRequired" placeholder="请选择" style="width: 100%;" class="filter-item">
            <el-option key="Y" label="是" value="Y" />
            <el-option key="N" label="否" value="N" />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="映射目标属性" min-width="100px" align="center">
        <template v-slot="{row}">
          <el-input v-model="row.resultTarget" placeholder="请输入" clearable />
        </template>
      </el-table-column>
      <el-table-column label="状态" min-width="70px" align="center">
        <template v-slot="{row}">
          <el-select v-model="row.status" placeholder="请选择" style="width: 100%;" class="filter-item">
            <el-option key="Y" label="启用" value="Y" />
            <el-option key="N" label="禁用" value="N" />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column v-if="state !== 'detail'" label="操作" align="center" fixed="right" width="100px" class-name="small-padding fixed-width">
        <template v-slot="{row,$index}">
          <el-button size="medium" type="text" icon="el-icon-circle-plus-outline" @click="handleAddRow(row, $index)" />
          <el-button size="medium" type="text" icon="el-icon-delete" @click="handleRemoveRow(row, $index)" />
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { handleList } from './api.js'
import permission from '@/directive/permission/index' // 权限判断指令
// import checkPermission from '@/utils/permission' // 权限判断函数
import waves from '@/directive/waves' // waves directive
// import { parseTime } from '@/utils'

export default {
  // 当前页面名称（组件）
  name: 'AtomicResultForm',
  // 引用组件
  components: { },
  directives: { permission, waves },
  filters: {},
  // props属性
  props: {
    resultFormData: {
      type: Object,
      require: true,
      default: undefined
    }
  },
  data() {
    return {
      // 列表table的key
      tableKey: 0,
      // 遮罩层
      loading: true,
      // table数据对象
      list: [],
      // table条件查询对象
      listQuery: {
        param: {
          serviceCode: undefined
        }
      },
      // 状态字典
      statusOptions: [{ code: 'Y', text: '启用' }, { code: 'N', text: '禁用' }],
      // 表单数据对象
      formData: {},
      // 表单校验规则
      rules: {
        // 按需调整
        id: [{ required: true, message: '主键不能为空', trigger: 'change' }],
        serviceCode: [{ required: true, message: '接口服务编码不能为空', trigger: 'change' }],
        resultSource: [{ required: true, message: '参数属性不能为空', trigger: 'change' }],
        resultName: [{ required: true, message: '参数名称不能为空', trigger: 'change' }],
        resultType: [{ required: true, message: '参数类型{"String":"字符串","Number":"数字"}不能为空', trigger: 'change' }],
        isRequired: [{ required: true, message: '是否必填{"1":"是","0":"否"}不能为空', trigger: 'change' }],
        resultTarget: [{ required: true, message: '映射目标属性不能为空', trigger: 'change' }],
        remark: [{ required: true, message: '备注不能为空', trigger: 'change' }],
        status: [{ required: true, message: '是否禁用状态不能为空', trigger: 'change' }]
      },
      state: ''
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    // 查询table数据对象
    handleInit() {
      this.state = this.resultFormData.state

      this.list = []
      this.listQuery.param.serviceCode = this.resultFormData.serviceCode
      this.loading = true
      handleList(this.listQuery.param).then(res => {
        this.list = res.data
        this.loading = false
      })
    },
    // 条件查询table数据
    handleFilterQuery() {
      this.handleInit()
    },
    // 新增一行
    handleAddRow(row, index) {
      const newRow = { resultSource: '', resultName: '', resultType: '', isRequired: '', resultTarget: '', status: 'Y' }
      this.$nextTick(() => {
        this.list.splice(index + 1, 0, newRow)
      })
    },
    // 删除选中行
    handleRemoveRow(row, index) {
      this.list.splice(index, 1)
    },
    // 获取table数据
    handleTableData() {
      return this.list
    }
  }
}
</script>
